<%--
  Created by IntelliJ IDEA.
  User: MSI
  Date: 2022/5/7
  Time: 10:49
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>

<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css" />
    <script type="text/javascript" src="/static/js/jquery.min.js"></script>
    <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=f4ef4553cd6af5d27baca941126690d7"></script>
    <style>
        html,
        body {
            height: 100%;
            margin: 0px;
            /*去掉默认边距*/
        }

        #cc {
            width: 100%;
            height: 100%;
        }

        .weather {
            width: 5rem;
            display: inline-block;
            padding-left: 0.5rem;
        }

        .sharp {
            height: 1rem;
            width: 1rem;
            background-color: white;
            transform: rotateZ(45deg);
            box-shadow: 2px 2px 3px rgba(114, 124, 245, .5);
            position: inherit;
            margin-left: 10.5rem;
            margin-top: -6px;
        }
    </style>
</head>

<body>

<div id="cc"></div>
<div class="info">
    <h4>未来四天预报天气</h4>
    <hr>
    <p id='forecast'></p>
</div>

<div class="input-card" style="position: absolute;left: 1px;top: 1px; height: 200px;">
    <h4>下属行政区查询</h4>
    <div class="input-item">
        <div class="input-item-prepend"><span class="input-item-text">省市区</span></div>
        <select id="province" style="width:100px" onchange='loadProv()'></select>
    </div>
    <div class="input-item">
        <div class="input-item-prepend"><span class="input-item-text">地级市</span></div>
        <select id='city' style="width:100px" onchange='loadCity()'></select>
    </div>

</div>
</body>

</html>
<script>
    //初始化地图
    var map = new AMap.Map('cc');

    var district;

    var pp;
    //加载地图查询插件
    AMap.plugin('AMap.DistrictSearch', function() {
        // 创建行政区查询对象
        district = new AMap.DistrictSearch()
        //查询中国下的省级名称
        district.search("中国", function(status, result) {
            var prov = result.districtList[0].districtList
            console.log(prov)
            var html = "<option value='0'>----请选择省份----</option>";
            for(var i = 0; i < prov.length; i++) {
                html += "<option value='" + prov[i].name + "'>" + prov[i].name + "</option>"
            }
            $("#province").html(html);
        })
    })

    //省级联动加载市区数据
    function loadProv() {
        var province = $("#province").val();
        //查询中国下的市级名称
        district.search(province, function(status, result) {
            var city = result.districtList[0].districtList
            console.log(city)
            var html = "<option value='0'>----请选择市----</option>";
            for(var i = 0; i < city.length; i++) {
                html += "<option value='" + city[i].name + "'>" + city[i].name + "</option>"
            }
            $("#city").html(html);
            //pp = result.districtList[0].center
        })
        //loadData(province);
    }

    //市级联动加载区数据
    function loadCity() {
        var city = $("#city").val();
        //查询中国下的市级名称
        district.search(city, function(status, result) {

            pp = result.districtList[0].center
        })

        loadData(city);
    }

    //查询地图
    function loadData(address) {

        //这里需要重新渲染
        var myMap = new AMap.Map('cc', {
            resizeEnable: true,
            center: pp,
            zoom: 15
        });
        console.log(1111);
        console.log(map.getCenter());
        AMap.plugin('AMap.Weather', function() {
            var weather = new AMap.Weather();
            //查询实时天气信息, 查询的城市到行政级别的城市，如朝阳区、杭州市
            weather.getLive(address, function(err, data) {
                if(!err) {
                    var str = [];
                    str.push('<h4 >实时天气' + '</h4><hr>');
                    str.push('<p>城市/区：' + data.city + '</p>');
                    str.push('<p>天气：' + data.weather + '</p>');
                    str.push('<p>温度：' + data.temperature + '℃</p>');
                    str.push('<p>风向：' + data.windDirection + '</p>');
                    str.push('<p>风力：' + data.windPower + ' 级</p>');
                    str.push('<p>空气湿度：' + data.humidity + '</p>');
                    str.push('<p>发布时间：' + data.reportTime + '</p>');
                    var marker = new AMap.Marker({
                        map: myMap,
                        position: pp
                    });
                    var infoWin = new AMap.InfoWindow({
                        content: '<div class="info" style="position:inherit;margin-bottom:0;">' + str.join('') + '</div><div class="sharp"></div>',
                        isCustom: true,
                        offset: new AMap.Pixel(0, -37)
                    });
                    infoWin.open(myMap, marker.getPosition());
                    marker.on('mouseover', function() {
                        infoWin.open(myMap, marker.getPosition());
                    });
                }
            });

            weather.getForecast(address, function(err, data) {
                if(err) {
                    return;
                }
                var str = [];
                for(var i = 0, dayWeather; i < data.forecasts.length; i++) {
                    dayWeather = data.forecasts[i];
                    str.push(dayWeather.date + ' <span class="weather">' + dayWeather.dayWeather + '</span> ' + dayWeather.nightTemp + '~' + dayWeather.dayTemp + '℃');
                }
                document.getElementById('forecast').innerHTML = str.join('<br>');
            });
        })
    }
</script>